﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Customer_Profile.eCard.DBDao;
using KVB.Common;
using Customer_Profile.Admin;

namespace Customer_Profile.eCard
{
    public partial class cpartySearch : System.Web.UI.Page
    {
        string strUserName = "";
        string strUserID = "";
        string strEntity = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            Comm.CheckSession(this.Page, "/login.aspx", "usermodel");
            if (Session["usermodel"] != null)
            {
                strUserID = ((UserInfoEntity)Session["usermodel"]).UserLoginId;
                strUserName = ((UserInfoEntity)Session["usermodel"]).UserName;
                strEntity = ((UserInfoEntity)Session["usermodel"]).UserOffice;
            }
            else
            {
                Log.WriteDefaultLog("main_01 Page_Load", "Session is null");
                return;
            }
           
            //if (!Page.IsPostBack)
            //{                
            //    bindGrid();
            //}
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            bindGrid();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("CpartyAdd.aspx");
        }

        private void bindGrid()
        {
            try
            {
                string strWhere = "";
                if (txtCpartyName.Text.Trim() != "")
                    strWhere += " and cparty_name like '" + txtCpartyName.Text.Trim() + "%'";
                if (txtIDNo.Text.Trim() != "")
                    strWhere += " and id_no = '" + txtIDNo.Text.Trim() + "'";

                //if (dropEntity.SelectedIndex>0)
                //    strWhere += " and entity = '" + dropEntity.SelectedItem.Value + "'";

                DataSet ds = CpartyDao.getCpartyList(strWhere);
                if (ds != null && ds.Tables.Count > 0)
                {
                    GvData.DataSource = ds.Tables[0];
                    GvData.DataBind();
                }
                else
                {
                    GvData.DataBind();
                }
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("bindGrid", ex.ToString());
            }
        }

        protected void GvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GvData.PageIndex = e.NewPageIndex;
            bindGrid();
        }

        protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lb = (Label)e.Row.Cells[0].Controls[1];

                ImageButton ibEdit = (ImageButton)e.Row.Cells[10].Controls[1];
                ImageButton ibTransfer = (ImageButton)e.Row.Cells[11].Controls[1];
                string dealno = ibTransfer.CommandArgument.ToString();
                //ibEdit.Attributes.Add("onclick", "javascript: window.open('CpartyEdit.aspx?cparty_id=" + lb.Text + "', 'newwindow'');return false");
                ibEdit.Attributes.Add("onclick", "javascript: var s_height = screen.height - 75; window.open('CpartyEdit.aspx?cparty_id=" + lb.Text + "', 'newwindow', 'width=' + screen.width + ',height=' +  s_height + ',top=0,left=0,toolbar=no,scrollbars=no,location=no,status=no');return false");
                ibTransfer.Attributes.Add("onclick", "javascript:window.open('CpartyBank.aspx?cparty_id=" + lb.Text + "');return false");
            }
        }

        protected void imgbt_Click(object sender, ImageClickEventArgs e)
        {
            string cardno = ((ImageButton)sender).Attributes["cardno"].ToString();
            Response.Redirect("transferSearchList.aspx?CARDNO=" + cardno, true);
        }
    }
}